﻿using System;
using System.Collections.Generic;
using System.Web;
using App_Code.Logic;
using SignalR.Hubs;

namespace App_Code.Objects
{
    /// <summary>
    /// SignalR Hub for Visual Cron
    /// These methods can be called fron Javascript code
    /// </summary>
    [HubName("visualCron")]
    public class VisualCronHub : Hub
    {
        public bool Start(string aspSessionId)
        {
            var vcSession = VisualCronLogic.VisualCronSessions.Find(x => x.AspNetSessionId == aspSessionId);
            if (vcSession != null)
            {
                vcSession.SignalRClientId = Context.ConnectionId;
                vcSession.ConnectionStateChanged();
                return true;
            }

            return false;
        }

        public void Connect(string connectionId, string username, string password)
        {
            try
            {
                var vcSession = VisualCronLogic.GetByClientId(Context.ConnectionId);

                if (vcSession == null)
                {
                    throw new ArgumentException("Context.ConnectionId");
                }

                var connection = ConnectionLogic.GetConnection(vcSession, connectionId);

                if (connection == null)
                {
                    throw new ArgumentException("connectionId");
                }

                // this is done, because we can override connection settings with username/password entered later
                // but we don't want to update connection with these data
                string connectUsername = string.IsNullOrEmpty(username) ? connection.Username : username;
                string connectPassword = string.IsNullOrEmpty(password) ? connection.Password : password;

                VisualCronLogic.Connect(connection.Host, connectUsername, connectPassword, vcSession,
                                        connection.IsLocalServer, connection.ConnectionId.ToString(),
                                        connection.UseCompression);
            }
            catch(Exception exc)
            {
                SignalRLogic.SendConnectionError(Context.ConnectionId, connectionId, false, exc.Message);
            }
        }

        public bool Disconnect(string connectionId)
        {
            var vcSession = VisualCronLogic.GetByClientId(Context.ConnectionId);

            if (vcSession != null)
            {
                var connection = ConnectionLogic.GetConnection(vcSession, connectionId);

                return VisualCronLogic.Disconnect(vcSession, connection.UserServerId);
            }

            return false;
        }

        public List<JobDTO> GetJobs(string serverId)
        {
            var vcSession = VisualCronLogic.GetByClientId(Context.ConnectionId);

            if (vcSession != null)
            {
                return vcSession.GetJobsDTO(serverId);
            }

            return null;
        }

        public void ActivateJob(string serverId, string jobId)
        {
            var vcSession = VisualCronLogic.GetByClientId(Context.ConnectionId);

            if (vcSession != null)
            {
                VisualCronLogic.JobActivate(vcSession, serverId, jobId);
            }
        }

        public void DeactivateJob(string serverId, string jobId)
        {
            var vcSession = VisualCronLogic.GetByClientId(Context.ConnectionId);

            if (vcSession != null)
            {
                VisualCronLogic.JobDeactivate(vcSession, serverId, jobId);
            }
        }

        public void RunJob(string serverId, string jobId, string taskId)
        {
            var vcSession = VisualCronLogic.GetByClientId(Context.ConnectionId);

            if (vcSession != null)
            {
                VisualCronLogic.JobStart(vcSession, serverId, jobId, taskId);
            }
        }

        public void StopJob(string serverId, string jobId)
        {
            var vcSession = VisualCronLogic.GetByClientId(Context.ConnectionId);

            if (vcSession != null)
            {
                VisualCronLogic.JobStop(vcSession, serverId, jobId);
            }
        }



        public List<TaskDTO> GetTasks(string serverId, string jobId)
        {
            var vcSession = VisualCronLogic.GetByClientId(Context.ConnectionId);

            if (vcSession != null)
            {
                return vcSession.GetTasksDTO(serverId, jobId);
            }

            return null;
        }

        public void ActivateTask(string serverId, string jobId, string taskId)
        {
            var vcSession = VisualCronLogic.GetByClientId(Context.ConnectionId);

            if (vcSession != null)
            {
                VisualCronLogic.TaskActivate(vcSession, serverId, taskId);
            }
        }

        public void DeactivateTask(string serverId, string jobId, string taskId)
        {
            var vcSession = VisualCronLogic.GetByClientId(Context.ConnectionId);

            if (vcSession != null)
            {
                VisualCronLogic.TaskDeactivate(vcSession, serverId, taskId);
            }
        }

        public void RunTask(string serverId, string jobId, string taskId)
        {
            var vcSession = VisualCronLogic.GetByClientId(Context.ConnectionId);

            if (vcSession != null)
            {
                VisualCronLogic.TaskStart(vcSession, serverId, jobId, taskId);
            }
        }

        public void StopTask(string serverId, string jobId, string taskId)
        {
            var vcSession = VisualCronLogic.GetByClientId(Context.ConnectionId);

            if (vcSession != null)
            {
                VisualCronLogic.TaskStop(vcSession, serverId, jobId, taskId);
            }
        }

        public List<VCConnection> AddUpdateConnection(string connectionId, string host, string username, string password, string port, bool isLocalServer, bool useCompression)
        {
            var vcSession = VisualCronLogic.GetByClientId(Context.ConnectionId);

            if (vcSession != null)
            {
                ConnectionLogic.AddUpdateConnection(vcSession, connectionId, host, username, password, port, isLocalServer, useCompression);

                return vcSession.Connections;
            }

            return null;
        }

        public List<VCConnection> RemoveConnection(string connectionId)
        {
            var vcSession = VisualCronLogic.GetByClientId(Context.ConnectionId);

            if (vcSession != null)
            {
                ConnectionLogic.RemoveConnection(vcSession, connectionId);

                return vcSession.Connections;
            }

            return null;
        }

        public void KeepAlive()
        {
            return;
        }
    }
}